package com.yy.finvideoplayer

import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import kotlin.math.abs

/**
 * @Description: HandleTouchEvent
 * @Author: yiw89
 * @CreateDate: 2025/9/1 15:18
 * @Version: 1.0
 */
class HandleTouchEvent internal constructor(touchEvent: GestureEvent) {
    private val gestureEvent = touchEvent

    private var moveLen = 0
    private val speedrate = 0.50f //调节进度灵敏度 滑动0.xx屏幕可以到底

    private var downX = 0f
    private var downY = 0f
    private var w = 0
    private var h = 0
    private var leftX = 0
    private var rightX = 0
    private var type = -1
    var level: Float = 0f

    private var tempTime: Long = 0

    fun handleEvent(view: View, event: MotionEvent): Boolean {
        val x = event.x
        val y = event.y

        if (moveLen == 0) moveLen = ViewConfiguration.get(view.context).scaledTouchSlop + 30

        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                init()
                downX = x
                downY = y
                w = view.width
                h = view.height
                leftX = (w * 0.25).toInt()
                rightX = (w * 0.75).toInt()
            }

            MotionEvent.ACTION_MOVE -> {
                if (downY < moveLen) return false

                val deltaX = x - downX
                val deltaY = y - downY

                //判断触发
                if (type < 0) {
                    val absDeltaX = abs(deltaX.toDouble()).toFloat()
                    val absDeltaY = abs(deltaY.toDouble()).toFloat()
                    if (absDeltaX > moveLen) {
                        type = GestureEvent.TOUCH_FULL_X
                        downX = x
                        gestureEvent.onGestureBegin(type)
                    }
                    if (absDeltaY > moveLen && downX <= leftX) {
                        type = GestureEvent.TOUCH_LEFT_Y
                        downY = y
                        gestureEvent.onGestureBegin(type)
                    }
                    if (absDeltaY > moveLen && downX > rightX) {
                        type = GestureEvent.TOUCH_RIGHT_Y
                        downY = y
                        gestureEvent.onGestureBegin(type)
                    }
                }

                when (type) {
                    GestureEvent.TOUCH_FULL_X -> {
                        level = 1.0f * deltaX / (w * speedrate)
                        //val speedFactor = calculateSpeedFactor(getDuration()) // 根据视频时长计算速度因子
                        //level = 1.0f * deltaX / (w * speedrate * speedFactor)
                    }
                    GestureEvent.TOUCH_LEFT_Y -> level = 1.0f * -deltaY / (h * speedrate)
                    GestureEvent.TOUCH_RIGHT_Y -> level = 1.0f * -deltaY / (h * speedrate)
                }
                if (type > 0) {
                    if (level < -1) level = -1f
                    if (level > 1) level = 1f
                    gestureEvent.onGestureChange(type, level)
                }
            }

            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                if (downY < moveLen) return false

                val l = System.currentTimeMillis()
                val delay = l - tempTime
                tempTime = l
                if (delay < 300) type = GestureEvent.TOUCH_DOUBLE_C

                if (type > 0) view.post { gestureEvent.onGestureEnd(type, level) }
            }
        }
        return (type > 0) and (type != GestureEvent.TOUCH_DOUBLE_C) //双击不拦截事件
    }

    // 新增速度因子计算函数
    private fun calculateSpeedFactor(duration: Long): Float {
        return when {
            duration <= 60000 -> 0.5f // 1分钟以内：慢速
            duration <= 300000 -> 1.0f // 1-5分钟：正常速度
            duration <= 1800000 -> 2.0f // 5-30分钟：快速
            else -> 3.0f // 30分钟以上：超快速
        }
    }

    private fun init() {
        downY = 0f
        downX = downY
        rightX = 0
        leftX = rightX
        w = 0
        h = w
        type = -1
        level = 0f
    }

    interface GestureEvent {
        //...
        fun onGestureBegin(type: Int) //触发手势事件

        fun onGestureChange(type: Int, level: Float) //改变level: -1 ~ 1

        fun onGestureEnd(type: Int, level: Float) //手势事件结束

        companion object {
            const val TOUCH_FULL_X: Int = 1 //全屏左右滑动
            const val TOUCH_LEFT_Y: Int = 2 //左边上下滑动
            const val TOUCH_RIGHT_Y: Int = 3 //右边上下滑动
            const val TOUCH_DOUBLE_C: Int = 4 //双击
        }
    }
}
